package com.ruoyi.purchase.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.purchase.domain.HistoryPlanParams;
import com.ruoyi.purchase.domain.PurchasePlan;
import com.ruoyi.purchase.mapper.PurchasePlanMapper;
import com.ruoyi.purchase.service.IPurchasePlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.View;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 采购计划Service业务层处理
 *
 * @author kevinness
 * @date 2024-12-29
 */
@Service
public class PurchasePlanService implements IPurchasePlanService
{
    @Autowired
    private PurchasePlanMapper purchasePlanMapper;
    @Autowired
    private View error;

    /**
     * 查询采购计划
     *
     * @param planNo 采购计划主键
     * @return 采购计划
     */
    @Override
    public PurchasePlan selectPurchasePlanByPlanNo(String planNo)
    {
        return purchasePlanMapper.selectPurchasePlanByPlanNo(planNo);
    }

    /**
     * 查询采购计划列表
     *
     * @param purchasePlan 采购计划
     * @return 采购计划
     */
    @Override
    public List<PurchasePlan> selectPurchasePlanList(PurchasePlan purchasePlan)
    {
        return purchasePlanMapper.selectPurchasePlanList(purchasePlan);
    }

    /**
     * 查询最新生成的采购计划
     *
     * @return 采购计划集合
     */
    @Override
    public List<PurchasePlan> selectLatestPurchasePlan(PurchasePlan purchasePlan){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
        LocalDate today = LocalDate.now();
        LocalDate firstDayOfMonth = today.withDayOfMonth(1);
        String formattedFirstDay = firstDayOfMonth.format(formatter);
        purchasePlan.setCycleNo(formattedFirstDay);
        return purchasePlanMapper.selectPurchasePlanList(purchasePlan);
    }

    /**
     * 查询最后一条采购计划
     *
     * @return PurchasePlan
     */
    public PurchasePlan selectFinalPurchasePlan(){
        return purchasePlanMapper.selectLatestPurchasePlan();
    }

    /**
     * 格式化输出采购计划统计信息
     *
     * @return List<HistoryPlanParams>
     */
    @Override
    public List<HistoryPlanParams> getStatisticsInfo(String cycleNo){
        PurchasePlan plan = new PurchasePlan();
        plan.setCycleNo(cycleNo);
        List<PurchasePlan> purchasePlans =
                purchasePlanMapper.selectPurchasePlanList(plan);
        List<HistoryPlanParams> historyPlanParamsList = new ArrayList<HistoryPlanParams>();
        for (PurchasePlan purchasePlan : purchasePlans) {
            historyPlanParamsList.add(new HistoryPlanParams(purchasePlan));
        }
        return historyPlanParamsList;
    }

    /**
     * 新增采购计划
     *
     * @param purchasePlan 采购计划
     * @return 结果
     */
    @Override
    public int insertPurchasePlan(PurchasePlan purchasePlan)
    {
        return purchasePlanMapper.insertPurchasePlan(purchasePlan);
    }

    /**
     * 修改采购计划
     *
     * @param purchasePlan 采购计划
     * @return 结果
     */
    @Override
    public int updatePurchasePlan(PurchasePlan purchasePlan)
    {
        return purchasePlanMapper.updatePurchasePlan(purchasePlan);
    }

    /**
     * 批量删除采购计划
     *
     * @param planNos 需要删除的采购计划主键
     * @return 结果
     */
    @Override
    public int deletePurchasePlanByPlanNos(String[] planNos)
    {
        return purchasePlanMapper.deletePurchasePlanByPlanNos(planNos);
    }

    /**
     * 删除采购计划信息
     *
     * @param planNo 采购计划主键
     * @return 结果
     */
    @Override
    public int deletePurchasePlanByPlanNo(String planNo)
    {
        return purchasePlanMapper.deletePurchasePlanByPlanNo(planNo);
    }

    /**
     * 需求预测
     */
    public int demandForcast(String cycleNo){
        PurchasePlan plan = new PurchasePlan();
        plan.setCycleNo(cycleNo);
        List<PurchasePlan> purchasePlans = purchasePlanMapper.selectPurchasePlanList(plan);
        for (PurchasePlan purchasePlan : purchasePlans) {
            deletePurchasePlanByPlanNo(purchasePlan.getPlanNo());
        }

        PurchasePlan plan1 = new PurchasePlan(cycleNo + "001",cycleNo,"RP001","牛肉",400.0,1000.0,100.0,LocalDate.now().toString(),LocalDate.now().plusWeeks(2).toString(),LocalDate.now().toString(),LocalDate.now().toString(),"operator","无");
        PurchasePlan plan2 = new PurchasePlan(cycleNo + "002",cycleNo,"RP002","猪肉",700.0,1000.0,100.0,LocalDate.now().toString(),LocalDate.now().plusWeeks(2).toString(),LocalDate.now().toString(),LocalDate.now().toString(),"operator","无");
        PurchasePlan plan3 = new PurchasePlan(cycleNo + "003",cycleNo,"RP005","三文鱼",300.0,1000.0,100.0,LocalDate.now().toString(),LocalDate.now().plusWeeks(2).toString(),LocalDate.now().toString(),LocalDate.now().toString(),"operator","无");
        purchasePlanMapper.insertPurchasePlan(plan1);
        purchasePlanMapper.insertPurchasePlan(plan2);
        return purchasePlanMapper.insertPurchasePlan(plan3);
    }
}
